import { Layers, Node, SpriteFrame, UITransform} from 'cc';

//创建并配置UI节点
export const createUINode = (name: string = '') => {
  const node = new Node(name)
  const transform = node.addComponent(UITransform)
  transform.setAnchorPoint(0, 1)
  node.layer = 1 << Layers.nameToLayer("UI_2D")

  return node
}

export const randomByLen=(len:number)=>
  Array.from({ length: len }).reduce<string>((total, item) => total + Math.floor(Math.random() * 10).toString(), '')

//生成指定范围的随机整数
export const randomByRange=(start:number,end :number):number=>Math.floor(start+(end-start)*Math.random())

const reg=/\((\d+)\)/

const getNumberWithString=(str:string)=>parseInt((str.match(reg)&&str.match(reg)[1])||'0')

//对帧动画数组进行排序

export const sortSpriteFrame=(spriteFrames:SpriteFrame[])=>
  spriteFrames.sort((a,b)=>getNumberWithString(a.name)-getNumberWithString(b.name))

